package com.java.zhuiyun.api.sysrole.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.zhuiyun.api.sysmenu.entity.Menu;
import com.java.zhuiyun.api.sysmenu.mapper.MenuMapper;
import com.java.zhuiyun.api.sysmenu.service.IMenuService;
import com.java.zhuiyun.api.sysrole.entity.Role;
import com.java.zhuiyun.api.sysrole.mapper.RoleMapper;
import com.java.zhuiyun.api.sysrole.service.IRoleService;
import com.java.zhuiyun.api.sysrolemenu.entity.RoleMenu;
import com.java.zhuiyun.api.sysrolemenu.mapper.RoleMenuMapper;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName RoleServiceImpl
 * @Description TODO
 * @Author xy
 * @Date 2023/8/16 15:46
 **/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    RoleMapper roleMapper;

    @Resource
    RoleMenuMapper roleMenuMapper;

    @Resource
    IMenuService menuService;

    @Resource
    MenuMapper menuMapper;

    /**
    * @Author xy
    * @Description 全查
    * @Date 2023/8/16 16:00
    * @Return  * @param null
    */
    @Override
    public PageResult RoleList(Role role, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(roleMapper.RoleList(role));
        return pageResult;
    }

    /**
    * @Author xy
    * @Description 添加
    * @Date 2023/8/16 16:00
    * @Return  * @param null
    */
    @Override
    public int RoleAdd(Role role) {
        // 获取当前时间
        Date date = new Date();
        // 添加时间时获取当前时间
        role.setCreateTime(date);
        // 添加时字段del_flag默认为0
        role.setDelFlag(0);
        return roleMapper.RoleAdd(role);
    }

    /**
    * @Author xy
    * @Description 修改
    * @Date 2023/8/16 16:00
    * @Return  * @param null
    */
    @Override
    public int RoleUpdate(Role role) {
        // 获取当前时间
        Date date = new Date();
        // 获取当前修改时间
        role.setUpdateTime(date);
        return roleMapper.Roleupdate(role);
    }

    /**
    * @Author xy
    * @Description 逻辑删除
    * @Date 2023/8/16 16:00
    * @Return  * @param null
    */
    @Override
    public int RoleDelete(Integer id) {
        return roleMapper.RoleDelete(id);
    }

    @Transactional
    @Override
    public void setRoleMenu(Integer roleId, List<Integer> menuIds) {
        // 删除当前角色id所有的绑定关系
        roleMenuMapper.deleteByRoleId(roleId);
        // Mark the parent menu IDs as added
        List<Integer> addedParentMenuIds = new ArrayList<>();
        // 把前端传过来的菜单id数组绑定到当前的角色id上
        for (Integer menuId : menuIds) {
            // 获取当前菜单id的信息
            Menu menu = menuMapper.getById(menuId);
            // 二级菜单 并且传过来的menuId数组里面没有它的父级id
            if (menu.getPid() != null && !menuIds.contains(menu.getPid()) && !addedParentMenuIds.contains(menu.getPid())) {
                // 补上这个父级id
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menu.getPid());
                Integer integer = roleMenuMapper.inserts(roleMenu);
                if (integer == 0) {
                    throw new RuntimeException("添加失败");
                }
                // Mark the parent menu ID as added
                addedParentMenuIds.add(menu.getPid());
            }
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenuMapper.inserts(roleMenu);
        }
    }

    @Override
    public List<Integer> getRoleMenu(Integer roleId) {
        return roleMenuMapper.selectByRoleId(roleId);
    }
}
